package com.example.lots_of_demo.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.lots_of_demo.model.Menu;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface MenuDao extends BaseMapper<Menu> {

    /**
     * 根据用户账户查找 用户拥有的菜单
     * @param username 用户账户
     * @return List<Menu>
     */
    @Select("SELECT m.* FROM menu m left join permission p on m.permission_code = p.`code` left join r_role_permission rrp on p.id = rrp.permission_id left join r_user_role rur on rrp.role_id = rur.role_id left join `user` u on u.id = rur.user_id where u.username = #{username} and m.`enable` = 1 " +
            "UNION " +
            "SELECT m.* FROM menu m where ISNULL(m.parent_code) and m.`code` " +
            "in (SELECT m.parent_code FROM menu m left join permission p on m.permission_code = p.`code` left join r_role_permission rrp on p.id = rrp.permission_id left join r_user_role rur on rrp.role_id = rur.role_id left join `user` u on u.id = rur.user_id where u.username = #{username} and m.`enable` = 1)")
    @ResultType(Menu.class)
    List<Menu> getMenu(String username);

}